<?php
namespace app\common\model;
use app\common\model\BaseModel;
use think\Db;

/**
 * 
 */
class FieldModel extends BaseModel
{
	public function add($data)
	{
		$res=$this->allowField(true)->save($data);
		if ($res) {
			$field=$this->returnField($data);
			Db::query("alter table cqz_".$data['table_name']." add ".$field);
			return 1;
		} 
	}

	public function edit($data)
	{
		$field=$this->find($data['id']);
		$res=$field->isUpdate(true)->allowField(true)->save($data);
		if ($res) {
			$field_sql=$this->returnField($data);
			Db::query("alter table cqz_".$field->getData('table_name')." change ".$data['oldname']." ".$field_sql);
			return 1;
		}else{
			$this->error='更新失败';
			return;
		}
	}

	public function del($id)
	{
		$field=$this->find($id);
		$res=$field->delete();
		if ($res) {
            Db::query("alter table cqz_".$field->getData('table_name')." drop COLUMN ".$field->getData('name'));
			return 1;
		}else{
			return ;
		}
	}


    // 删除表的时候删除字段
    public function delTableField($table_name)
    {
        $res=$this->where("table_name='".$table_name."'")->delete();
        if ($res) {
            return 1;
        }else{
            return;
        }
    }

    // 是否输入
    public function isInput($data=[])
    {
        $field=$this->find($data['id']);
        $res=$field->save([
            $data['field']=>$data['value']
        ]);
        if ($res) {
            return 1;
        }else{
            $this->error=$res->getError();
        }
    }

	// 返回字段表单
    public function getForm($data)
    { 
    	switch ($data['f_form']) {
    		case 'text':
    			$html='<div class="form-group">
    <label class="col-xs-2">'.$data['title'].'</label>
    <div class="col-xs-10">
    	<input type="text" class="form-control"  name="'.$data['name'].'" placeholder="'.$data['title'].'" value="{$r.'.$data['name'].'}" datatype="'.$data['valid'].'">
    </div>
</div>';
    		break;
    		case 'password':
    			$html='<div class="form-group">
    <label class="col-xs-2">'.$data['title'].'</label>
    <div class="col-xs-10">
    	<input type="password" class="form-control"  name="'.$data['name'].'"  datatype="'.$data['valid'].'">
    </div>
</div>';
    		break;
    		case 'select':
    			$html='<div class="form-group">
    <label class="col-xs-2">'.$data['title'].'</label>
    <div class="col-xs-10">
    	<select name="'.$data['name'].'" class="form-control">
            '.$data['s_value'].'
    	</select>
    </div>
</div>';
    		break;
    		case 'radio':
    			$html='<div class="form-group">
    <label class="col-xs-2">'.$data['title'].'</label>
    <div class="col-xs-10">
    	'.$data['s_value'].'
    </div>
</div>';
    		break;
    		case 'checkbox':
    			$html='<div class="form-group">
    <label class="col-xs-2">'.$data['title'].'</label>
    <div class="col-xs-10">
    	'.$data['s_value'].'
    </div>
</div>';
    		break;
    		case 'textarea':
    			$html='<div class="form-group">
    <label class="col-xs-2">'.$data['title'].'</label>
    <div class="col-xs-10">
    	<textarea name="'.$data['name'].'" class="form-control" style="height: 200px;" placeholder="'.$data['title'].'"  datatype="'.$data['valid'].'">{$r.'.$data['name'].'}</textarea>
    </div>
</div>';
    		break;
    		case 'editor':
    			$html='<div class="form-group">
    <label class="col-xs-2">'.$data['title'].'</label>
    <div class="col-xs-10">
    	<script id="'.$data['name'].'" name="'.$data['name'].'" type="text/plain">{$r.'.$data['name'].'}</script>
    </div>
</div>';
    		break;
    		case 'img':
                $html='<div class="form-group">
    <label class="col-xs-2">'.$data['title'].'</label>
    <div class="col-xs-10">
        <input type="file"  name="'.$data['name'].'"  datatype="'.$data['valid'].'">
    </div>
    {:getPhotoList($r.'.$data['name'].')}
</div>';
            break;
            case 'moreimg':
    			$html='<div class="form-group">
    <label class="col-xs-2">'.$data['title'].'</label>
    <div class="col-xs-10">
    	<input type="file"  name="'.$data['name'].'[]"  datatype="'.$data['valid'].'">
    </div>
    {:getPhotoList($r.'.$data['name'].','.$table.',$r.id,'.$data['name'].')}
</div>';
    		break;
    		case 'date':
    			$html='<div class="form-group">
    <label class="col-xs-2">'.$data['title'].'</label>
    <div class="col-xs-10">
    	<input type="text" class="form-control"  name="'.$data['name'].'" id="'.$data['name'].'" placeholder="'.$data['title'].'" value="{$r.'.$data['name'].'}" datatype="'.$data['valid'].'">
    </div>
</div>';
    		break;
    	}

    	return $html;
    }


    // 生成表单
    public function createForm($table_name)
    {
        $file_admin=ROOT_PATH.'/application/admin/view/info/add_'.$table_name.'.html';
        $file_index=ROOT_PATH.'/application/index/view/info/add_'.$table_name.'.html';
         
            file_put_contents($file_admin, '{extend name="base"} 
{block name="content"}
<div class="box1">
    <form class="form-horizontal" method="post" enctype="multipart/form-data">
    '.$this->formHtml($table_name,1).'
    <div class="form-group"> 
            <div class="col-md-10 col-md-offset-2 col-xs-12">
                <input type="submit" class="btn btn-primary" value="确认提交">
            </div>
    </div>
    </form>
</div>
{/block}', LOCK_EX); 
        
            file_put_contents($file_index, '{extend name="base"} 
{block name="content"}
<div class="box1">
    <form class="form-horizontal" method="post" enctype="multipart/form-data">
    '.$this->formHtml($table_name,2).'
    <div class="form-group"> 
            <div class="col-md-10 col-md-offset-2 col-xs-12">
                <input type="submit" class="btn btn-primary" value="确认提交">
            </div>
        </div>
    </form>
</div>
{/block}', LOCK_EX);
         
    }

    // 获取所有字段表单的html
    private function formHtml($table_name,$q_h)
    {
        $html='';
        $arr=db('field')->where("table_name='".$table_name."'")->order('myorder asc,id asc')->select();
        foreach ($arr as $v) {
            switch ($q_h) {
                case '1'://后台
                    $html.=$v['h_html'].'
                    ';
                    break;
                case '2'://前台
                    $html.=$v['q_html'].'
                    ';
                    break; 
            }
        }
        return $html;
    }

	//返回字段类型
	public function returnField($add){
		if($add['type']=="TINYINT"||$add['type']=="SMALLINT"||$add['type']=="INT"||$add['type']=="BIGINT"||$add['type']=="FLOAT"||$add['type']=="DOUBLE")
		{
			$def=" default '0'";
		}
		elseif($add['type']=="VARCHAR"||$add['type']=="CHAR")
		{
			$def=" default ''";
		}
		elseif($add['type']=="DATE")
		{
			$def=" default '0000-00-00'";
		}
		elseif($add['type']=="DATETIME")
		{
			$def=" default '0000-00-00 00:00:00'";
		}
		else
		{
			$def='';
		}
		$type=$add['type'];
		//VARCHAR
		if(($add['type']=='VARCHAR'||$add['type']=='CHAR')&&empty($add['length']))
		{
			$add['length']='255';
		}
		//字段长度
		if($add['length'])
		{
			if($add['type']!="TEXT"&&$add['type']!="MEDIUMTEXT"&&$add['type']!="LONGTEXT"&&$add['type']!="DATE"&&$add['type']!="DATETIME")
			{
				$type.="(".$add['length'].")";
			}
		}
		$field=$add['name']." ".$type." NOT NULL".$def;
		return $field;
	}	
}
